package com.yoona.common.response;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.yoona.common.enums.CommonEnums;
import lombok.Data;

/**
 * @author: yoonada
 * @email: m15602498163@163.com
 * @create: 2020-10-29 14:41
 * @description: 自定义响应类
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class BaseResponse<T> {

    /**
     * 状态码
     */
    private int code;

    /**
     * 消息
     */
    private String msg;

    /**
     * 数目
     */
    private Integer amount;

    /**
     * 整个结果(Object)
     */
    private T result;

    public static final int SUCCESS =200;
    public static final String SUCCESS_MSG="请求成功";

    public BaseResponse() {
    }

    public BaseResponse(CommonEnums commonEnums) {
        this.code = commonEnums.getCode();
        this.msg = commonEnums.getMsg();
    }

    public BaseResponse(CommonEnums commonEnums, T result) {
        this.code = commonEnums.getCode();
        this.msg = commonEnums.getMsg();
        this.result = result;
    }

    public BaseResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public BaseResponse(int code, String msg, T result) {
        this.code = code;
        this.msg = msg;
        this.result = result;
    }

    public BaseResponse(int code, String msg, Integer amount, T result) {
        this.code = code;
        this.msg = msg;
        this.amount = amount;
        this.result = result;
    }

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/29 3:41 下午
     * @Param: [result]
     * @Return: com.yoona.common.response.BaseResponse<T>
     * @Description: 响应成功(传入result)
     */
    public static <T> BaseResponse<T> success(T result){
        BaseResponse<T> response = new BaseResponse<T>();
        response.setCode(SUCCESS);
        response.setMsg(SUCCESS_MSG);
        response.setResult(result);
        return response;
    }

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/29 3:41 下午
     * @Param: [result, amount]
     * @Return: com.yoona.common.response.BaseResponse<T>
     * @Description: 响应成功(传入result和amount)
     * 常用于嵌套List
     * BaseResponse<List<T>> list
     * BaseResponse.success(list,list.size());
     */
    public static <T> BaseResponse<T> success(T result, Integer amount){
        BaseResponse<T> response = new BaseResponse<T>();
        response.setCode(SUCCESS);
        response.setMsg(SUCCESS_MSG);
        response.setResult(result);
        response.setAmount(amount);
        return response;
    }

    public static <T> BaseResponse<T> success(CommonEnums commonEnums, T result){
        return new BaseResponse<T>(commonEnums,result);
    }

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/30 10:55 上午
     * @Param: [commonEnums, result]
     * @Return: com.yoona.common.response.BaseResponse<T>
     * @Description:
     */
    public static <T> BaseResponse<T> error(CommonEnums commonEnums, T result){
        return new BaseResponse<T>(commonEnums,result);
    }

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/29 3:53 下午
     * @Param: [commonEnums]
     * @Return: com.yoona.common.response.BaseResponse<T>
     * @Description: 响应成功(传入枚举)
     */
    public static <T> BaseResponse<T> success(CommonEnums commonEnums){
        return new BaseResponse<T>(commonEnums);
    }

    /**
     * @Author: YoonaDa
     * @Email: m15602498163@163.com
     * @CreateTime: 2020/10/29 4:12 下午
     * @Param: [commonEnums]
     * @Return: com.yoona.common.response.BaseResponse<T>
     * @Description: 响应失败(传入枚举)
     */
    public static <T> BaseResponse<T> error(CommonEnums commonEnums){
        return new BaseResponse<T>(commonEnums);
    }

}

